package com.android.component;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class MainActivity extends Activity implements View.OnClickListener {
    ListView listView;
    LayoutInflater layoutInflater;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.lv);
        listView.setAdapter(new MainAdapter());
        layoutInflater = getLayoutInflater();
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {
        MainItem item = (MainItem) view.getTag();
        startActivity(new Intent(this, item.activity));
    }

    enum Type {
        LAYOUT_TEST,
        SERVICE_TEST,
        TEXTVIEW_TEST,
        HANDLER_THREAD_TEST,
        TIMER_TEST,
        VIEW_TO_BMP
    }


    class MainItem {
        MainItem(Type type, String str, Class<? extends Activity> activity) {
            this.str = str;
            this.type = type;
            this.activity = activity;
        }

        String str;
        Type type;
        Class<? extends Activity> activity;
    }

    class MainAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return (items.length + 1) / 2;
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if (view == null) {
                view = layoutInflater.inflate(R.layout.item_main, null);
            }
            TextView tv1 = (TextView) view.findViewById(R.id.tv1);
            TextView tv2 = (TextView) view.findViewById(R.id.tv2);

            tv1.setText(items[i * 2].str);
            tv1.setTag(items[i * 2]);
            tv1.setOnClickListener(MainActivity.this);
            if ((i * 2 + 1) < items.length) {
                tv2.setText(items[i * 2 + 1].str);
                tv2.setTag(items[i * 2 + 1]);
                tv2.setOnClickListener(MainActivity.this);
            } else {
                tv2.setVisibility(view.INVISIBLE);
            }
            return view;

        }
    }

    MainItem items[] = new MainItem[]{
            new MainItem(Type.HANDLER_THREAD_TEST, "Handler Thread", ThreadWithHandlerTest.class),
            new MainItem(Type.LAYOUT_TEST, "Layout Test", Layouts.class),
            new MainItem(Type.SERVICE_TEST, "Service Test", ServiceTest.class),
            new MainItem(Type.TEXTVIEW_TEST, "TextView Test", TextViewTest.class),
            new MainItem(Type.TIMER_TEST, "Timer Test", TimerTest.class),


            new MainItem(Type.TIMER_TEST, "Timer Test", MeidaPlayTest.class),
            new MainItem(Type.TIMER_TEST, "SendMailTest", SendMailTest.class),
            new MainItem(Type.TIMER_TEST, "DrawTest", DrawTest.class),
            new MainItem(Type.TIMER_TEST, "ThreadPoolTest", ThreadPoolTest.class),
            new MainItem(Type.TIMER_TEST, "AnimationTest", AnimationTest.class),
            new MainItem(Type.TIMER_TEST, "JSonMapTest", JSonMapTest.class),

    };


}

